<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品别名" prop="alias">
        <el-input
          v-model="queryParams.alias"
          placeholder="请输入产品别名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['demo:product:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['demo:product:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['demo:product:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['demo:product:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="产品名称" align="center" prop="name" min-width="120">
        <template slot-scope="scope">
          <el-tooltip 
            v-if="scope.row.name && scope.row.name.length > 20" 
            effect="dark" 
            :content="scope.row.name" 
            placement="top"
            popper-class="custom-tooltip"
          >
            <span>{{ truncateText(scope.row.name, 20) }}</span>
          </el-tooltip>
          <span v-else>{{ scope.row.name || '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="产品别名" align="center" prop="alias" min-width="120">
        <template slot-scope="scope">
          <el-tooltip 
            v-if="scope.row.alias && scope.row.alias.length > 20" 
            effect="dark" 
            :content="scope.row.alias" 
            placement="top"
            popper-class="custom-tooltip"
          >
            <span>{{ truncateText(scope.row.alias, 20) }}</span>
          </el-tooltip>
          <span v-else>{{ scope.row.alias || '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="产品价格" align="center" prop="price">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.price">{{ scope.row.price }}元</el-tag>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="规格" align="center" prop="specification" min-width="120">
        <template slot-scope="scope">
          <el-tooltip 
            v-if="scope.row.specification && scope.row.specification.length > 20" 
            effect="dark" 
            :content="scope.row.specification" 
            placement="top"
            popper-class="custom-tooltip"
          >
            <span>{{ truncateText(scope.row.specification, 20) }}</span>
          </el-tooltip>
          <span v-else>{{ scope.row.specification || '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="封面图片" align="center" prop="image" min-width="120"> 
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" width="100" height="100" />
        </template>
      </el-table-column>
      <el-table-column label="核心成分" align="center" prop="coreComponents" min-width="120">
        <template slot-scope="scope">
          <el-tooltip 
            v-if="scope.row.coreComponents && scope.row.coreComponents.length > 20" 
            effect="dark" 
            :content="scope.row.coreComponents" 
            placement="top"
            popper-class="custom-tooltip"
          >
            <span>{{ truncateText(scope.row.coreComponents, 20) }}</span>
          </el-tooltip>
          <span v-else>{{ scope.row.coreComponents || '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="主要功效" align="center" prop="mainEffects" min-width="120">
        <template slot-scope="scope">
          <el-tooltip 
            v-if="scope.row.mainEffects && scope.row.mainEffects.length > 20" 
            effect="dark" 
            :content="scope.row.mainEffects" 
            placement="top"
            popper-class="custom-tooltip"
          >
            <span>{{ truncateText(scope.row.mainEffects, 20) }}</span>
          </el-tooltip>
          <span v-else>{{ scope.row.mainEffects || '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="适用人群" align="center" prop="suitableCrowd" min-width="120">
        <template slot-scope="scope">
          <el-tooltip 
            v-if="scope.row.suitableCrowd && scope.row.suitableCrowd.length > 20" 
            effect="dark" 
            :content="scope.row.suitableCrowd" 
            placement="top"
            popper-class="custom-tooltip"
          >
            <span>{{ truncateText(scope.row.suitableCrowd, 20) }}</span>
          </el-tooltip>
          <span v-else>{{ scope.row.suitableCrowd || '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作周期" align="center" prop="operationCycle" min-width="120">
        <template slot-scope="scope">
          <el-tooltip 
            v-if="scope.row.operationCycle && scope.row.operationCycle.length > 20" 
            effect="dark" 
            :content="scope.row.operationCycle" 
            placement="top"
            popper-class="custom-tooltip"
          >
            <span>{{ truncateText(scope.row.operationCycle, 20) }}</span>
          </el-tooltip>
          <span v-else>{{ scope.row.operationCycle || '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="效果呈现" align="center" prop="effectPresentation" min-width="120">
        <template slot-scope="scope">
          <el-tooltip 
            v-if="scope.row.effectPresentation && scope.row.effectPresentation.length > 20" 
            effect="dark" 
            :content="scope.row.effectPresentation" 
            placement="top"
            popper-class="custom-tooltip"
          >
            <span>{{ truncateText(scope.row.effectPresentation, 20) }}</span>
          </el-tooltip>
          <span v-else>{{ scope.row.effectPresentation || '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="产品分类" align="center" prop="category" min-width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.product_type" :value="scope.row.category" />
        </template>
      </el-table-column>
      <el-table-column label="产品原理" align="center" prop="principle" min-width="120">
        <template slot-scope="scope">
          <el-tooltip 
            v-if="scope.row.principle && scope.row.principle.length > 20" 
            effect="dark" 
            :content="scope.row.principle" 
            placement="top"
            popper-class="custom-tooltip"
          >
            <span>{{ truncateText(scope.row.principle, 20) }}</span>
          </el-tooltip>
          <span v-else>{{ scope.row.principle || '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="产品介绍" align="center" prop="introduction" min-width="120">
        <template slot-scope="scope">
          <el-tooltip 
            v-if="scope.row.introduction && scope.row.introduction.length > 20" 
            effect="dark" 
            :content="scope.row.introduction" 
            placement="top"
            popper-class="custom-tooltip"
          >
            <span>{{ truncateText(scope.row.introduction, 20) }}</span>
          </el-tooltip>
          <span v-else>{{ scope.row.introduction || '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="优先推荐" align="center" prop="priorityRecommendation">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_is_0_1" :value="scope.row.priorityRecommendation" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" min-width="120">
        <template slot-scope="scope">
          <el-tooltip 
            v-if="scope.row.remark && scope.row.remark.length > 20" 
            effect="dark" 
            :content="scope.row.remark" 
            placement="top"
            popper-class="custom-tooltip"
          >
            <span>{{ truncateText(scope.row.remark, 20) }}</span>
          </el-tooltip>
          <span v-else>{{ scope.row.remark || '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" min-width="120" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['demo:product:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['demo:product:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品管理抽屉 -->
    <el-drawer :title="title" :visible.sync="open" direction="ltr" size="60%" :before-close="cancel">
      <div class="drawer-content">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px" class="beautiful-form">
          <el-divider content-position="left">基本信息</el-divider>
          <el-form-item label="产品名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入产品名称" />
          </el-form-item>
          <el-form-item label="产品别名" prop="alias">
            <el-input v-model="form.alias" placeholder="请输入产品别名" />
          </el-form-item>
          <el-form-item label="产品价格" prop="price">
            <el-input-number v-model="form.price" placeholder="请输入产品价格" controls-position="right" :precision="2" :step="0.1" :min="0" style="width: 100%;"></el-input-number>
          </el-form-item>
          <el-form-item label="封面图片" prop="image"> 
            <image-upload v-model="form.image" :limit="1" />
          </el-form-item>
          
          <el-divider content-position="left">产品详情</el-divider>
          <el-form-item label="规格" prop="specification">
            <el-input v-model="form.specification" placeholder="请输入规格" />
          </el-form-item>
          <el-form-item label="核心成分" prop="coreComponents">
            <el-input v-model="form.coreComponents" type="textarea" :autosize="{ minRows: 2, maxRows: 4}" placeholder="请输入核心成分" />
          </el-form-item>
          <el-form-item label="主要功效" prop="mainEffects">
            <el-input v-model="form.mainEffects" type="textarea" :autosize="{ minRows: 2, maxRows: 4}" placeholder="请输入主要功效" />
          </el-form-item>
          <el-form-item label="适用人群" prop="suitableCrowd">
            <el-input v-model="form.suitableCrowd" type="textarea" :autosize="{ minRows: 2, maxRows: 4}" placeholder="请输入适用人群" />
          </el-form-item>
          <el-form-item label="操作周期" prop="operationCycle">
            <el-input v-model="form.operationCycle" placeholder="请输入操作周期" />
          </el-form-item>
          <el-form-item label="效果呈现" prop="effectPresentation">
            <el-input v-model="form.effectPresentation" type="textarea" :autosize="{ minRows: 2, maxRows: 4}" placeholder="请输入效果呈现" />
          </el-form-item>
          <el-form-item label="产品分类" prop="category">
            <el-select v-model="form.category" placeholder="请选择产品分类" style="width: 100%;">
              <el-option
                v-for="dict in dict.type.product_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-divider content-position="left">产品信息</el-divider>
          <el-form-item label="产品原理" prop="principle">
            <el-input v-model="form.principle" type="textarea" :autosize="{ minRows: 2, maxRows: 6}" placeholder="请输入产品原理" />
          </el-form-item>
          <el-form-item label="产品介绍" prop="introduction">
            <el-input v-model="form.introduction" type="textarea" :autosize="{ minRows: 2, maxRows: 6}" placeholder="请输入产品介绍" />
          </el-form-item>

          <el-form-item label="优先推荐" prop="priorityRecommendation">
            <el-select v-model="form.priorityRecommendation" placeholder="请选择优先推荐" style="width: 100%;">
              <el-option
                v-for="dict in dict.type.sys_is_0_1"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-divider content-position="left">其他信息</el-divider>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" :autosize="{ minRows: 2, maxRows: 4}" placeholder="请输入备注信息" />
          </el-form-item>
        </el-form>
        <div class="drawer-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { listProduct, getProduct, delProduct, addProduct, updateProduct } from "@/api/demo/product";

export default {
  name: "Product",
  dicts: ['product_type','sys_is_0_1'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        alias: null,
        price: null,
        specification: null,
        coreComponents: null,
        mainEffects: null,
        suitableCrowd: null,
        operationCycle: null,
        effectPresentation: null,
        category: null,
        principle: null,
        introduction: null,
        remark: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "产品名称不能为空", trigger: "blur" }
        ],
        alias: [
          { required: true, message: "产品别名不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "产品价格不能为空", trigger: "blur" },
          { type: 'number', min: 0, message: "产品价格必须大于等于0", trigger: "blur" }
        ],
       
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询产品列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        alias: null,
        price: null,
        specification: null,
        coreComponents: null,
        mainEffects: null,
        suitableCrowd: null,
        operationCycle: null,
        effectPresentation: null,
        category: null,
        principle: null,
        introduction: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProduct(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProduct(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除产品编号为"' + ids + '"的数据项？').then(function() {
        return delProduct(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('demo/product/export', {
        ...this.queryParams
      }, `product_${new Date().getTime()}.xlsx`)
    },
    /** 文字截断方法 */
    truncateText(text, length) {
      if (!text) return '-';
      return text.length > length ? text.substring(0, length) + '...' : text;
    }
  }
};
</script>

<style scoped>
.drawer-content {
  padding: 20px;
  height: 100%;
  box-sizing: border-box;
}

.beautiful-form {
  max-height: calc(100% - 48px);
  overflow-y: auto;
}

.drawer-footer {
  position: absolute;
  bottom: 20px;
  right: 20px;
}

.el-divider {
  margin: 30px 0 20px 0;
}

.el-form-item {
  margin-bottom: 20px;
}

/* 确保输入框宽度适应 */
.el-input-number {
  width: 100%;
}

/* 为必填项添加红色星号 */
.el-form-item.is-required .el-form-item__label:before {
  content: '*';
  color: #F56C6C;
  margin-right: 4px;
}

/* 表格中截断文本的样式 */
.el-table .cell {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>

<style>
/* 自定义tooltip样式，设置宽度为60% */
.custom-tooltip {
  max-width: 60% !important;
  word-break: break-word;
  white-space: normal;
  line-height: 1.5;
}
</style>